﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;

namespace Fabs.Net.GIL
{
    public class Class
    {
        public Class()
        {
            Attributes = new List<TypeAttributes>();
            Variables = new List<Variable>();
            Constructors = new List<Constructor>();
            Functions = new List<Function>();
        }

        public string Name { get; set; }
        public List<TypeAttributes> Attributes { get; set; }

        public List<Variable> Variables { get; set; }
        public List<Constructor> Constructors { get; set; }
        public List<Function> Functions { get; set; }

        public TypeBuilder Output { get; private set; }

        public bool HasEntryPoint { get; private set; }
        public MethodInfo EntryPoint { get; private set; }

        public void Compile(Executable Parent)
        {
            var parent = Parent.Output;

            TypeAttributes attributes = Attributes[0];
            for (int i = 1; i < Attributes.Count; i++)
            {
                attributes |= Attributes[i];
            }

            Output = parent.DefineType(Name, attributes);

            foreach (Variable var in Variables)
            {
                var.Compile(this);
            }

            foreach (Constructor con in Constructors)
            {
                con.Compile(this);
            }

            foreach (Function func in Functions)
            {
                func.Compile(this);
                if (!HasEntryPoint)
                {
                    if (func.IsEntryPoint)
                    {
                        HasEntryPoint = true;
                        EntryPoint = func.Output;
                    }
                }
            }
        }
    }
}